package com.dahong.cloud.authorize.controller;

import cn.hutool.json.JSONUtil;
import com.lambda.cloud.core.principal.LoginUser;
import com.lambda.cloud.core.utils.OperatorUtils;
import com.lambda.cloud.logger.annotation.OperationLog;
import com.lambda.cloud.logger.context.LogContext;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "HiController")
@RestController
@RequestMapping("/user")
public class HiController {

    @SuppressFBWarnings("EI_EXPOSE_REP2")
    private SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    public void setSimpMessagingTemplate(SimpMessagingTemplate simpMessagingTemplate) {
        this.simpMessagingTemplate = simpMessagingTemplate;
    }

    @GetMapping("/test")
    public String wsTest() {
        LoginUser loginUser = OperatorUtils.getOperator();
        simpMessagingTemplate.convertAndSend("/topic/hi", JSONUtil.createObj().append("operator", loginUser.getName()));
        return "ok";
    }

    @GetMapping("/testAdmin")
    public String testAdmin() {
        LoginUser loginUser = OperatorUtils.getOperator();
        simpMessagingTemplate.convertAndSendToUser(
                "admin", "/message", JSONUtil.createObj().append("operator", loginUser.getName()));
        return "ok";
    }

    @OperationLog("调用测试日志！！！！！")
    @GetMapping("/hi")
    public String hi() {
        LoginUser operator = OperatorUtils.getOperator();
        System.out.println(JSONUtil.toJsonPrettyStr(operator));
        return "async hi 我是 authorize-server 服务 当前登录用户：" + operator.getName() + "\n"
                + JSONUtil.toJsonPrettyStr(operator);
    }

    @OperationLog("调用测试日志，我把详情给修改了")
    @GetMapping("/hi2")
    public String hi2() {
        LogContext.setDetail("哈哈哈哈哈哈哈哈哈 ， 被修改了！");
        LoginUser operator = OperatorUtils.getOperator();
        System.out.println(JSONUtil.toJsonPrettyStr(operator));
        return "async hi 我是 authorize-server 服务 当前登录用户：" + operator.getName() + "\n"
                + JSONUtil.toJsonPrettyStr(operator);
    }
}
